#ifndef FILESENDER_H
#define FILESENDER_H

#include "fileHead.h"

#include <QObject>
#include <QString>
#include <QStringList>
#include <QFileInfo>
#include <QFile>
#include <QTcpSocket>
#include <QDateTime>

class FileSender : public QObject
{
    Q_OBJECT
    enum SendState{SEND_HEAD = 0, SEND_CONTENT, SEND_ERROR};
public:
    explicit FileSender(QObject *parent = 0);
    explicit FileSender(const QString &ip, quint16 port,
                        const QString &fileName, const QByteArray &passid,
                        QObject *parent = 0);

private:
    void addRelaiveFilePath(const QString &absoluteFilePath, const QString &relaiveFilePaht);

public:
    void sendFail(const QString &failInfo);
    void sendOk();

    void showSendState(){
        qDebug() << "当前发送的文件是" << m_sendFilePath + m_sendFileList.at(m_sendFileListIndex);
        qDebug() << QString("当前发送字节 (%1/%2) ,发送文件数 (%3/%4)")
                    .arg(m_totalBytesWritten).arg(m_totalFileSize)
                    .arg(m_sendFileListIndex).arg(m_sendFileList.size());
    }

public slots:
    //
    void onStateChanged(QAbstractSocket::SocketState socketState);
    //
    void onBytesWritten(qint64 bytes);


private:
    QTcpSocket  *m_socket;
    QString     m_sendFilePath;
    QStringList m_sendFileList;

    qint64      m_totalBytesWritten;
    int         m_sendFileListIndex;

    qint64      m_currentFileSize;
    qint64      m_currentFileBytesWritten;

    QFile       m_file;
    SendState   m_sendState;

    qint64      m_blockSize;
    QString     m_sendFileName;
    qint64      m_totalFileSize;
    QByteArray  m_passid;

    QDateTime   m_startTime;

};

#endif // FILESENDER_H
